//
//  LevelUpUIView.m
//  ent
//
//  Created by shirokuma on 15/5/12.
//  Copyright (c) 2015年 ND. All rights reserved.
//

#import "LevelUpUIView.h"
#import "UIView+WindowAlert.h"
#import <MUPFoundation/MUPFoundation.h>

@interface LevelUpUIView()
{
    UILabel * _levelLabel; //等级
    UILabel * _levelUpLabel; //排名飙升
    UILabel * _levelRankLabel; //榜单排名

}

@end
@implementation LevelUpUIView
-(instancetype)initWithFrame:(CGRect)frame level:(NSString *)level levelUp:(NSString *)levelUp rankUpCity:(NSString *)rankUpCity rankUp:(NSString *)rankUp{
    if(self = [super initWithFrame:frame]){
        [self initView:level levelUp:levelUp rankUpCity:rankUpCity rankUp:rankUp];
    }
    return self;
}


- (instancetype) initWithFrame:(CGRect)frame level:(NSString *)level upDesc:(NSString *)upDesc{
    if(self = [super initWithFrame:frame]){
        [self initView:level upDesc:upDesc];
    }
    return self;
}

-(void)initView:(NSString *)level levelUp:(NSString *)levelUp rankUpCity:(NSString *)rankUpCity rankUp:(NSString *)rankUp{
    
    self.backgroundImage = [UIImage imageNamed:@"levelUP"];
    
    self.centerY = CGRectGetMidY(self.bounds) + 50;
    
    CGFloat x = self.bounds.size.width;
    CGFloat y = self.bounds.size.height;
    _levelLabel = [[UILabel alloc]initWithFrame:CGRectMake(x / 2 - 60, y / 3 - 10, 110, 25)];
    _levelLabel.text = [NSString stringWithFormat:@"Lv.%@",level];
    _levelLabel.textAlignment = NSTextAlignmentCenter;
    _levelLabel.textColor = [UIColor colorWithRed:1 green:0.95 blue:0.2 alpha:1];
    _levelLabel.font = [UIFont boldSystemFontOfSize:26];
    [self addSubview: _levelLabel];
    
    _levelUpLabel = [[UILabel alloc]initWithFrame:CGRectMake(x / 2 - 72, y / 2 - 5, 180, 25)];
    
    NSMutableAttributedString * levelUpStr1 = [[NSMutableAttributedString alloc] initWithString:@"粉丝排名飙升了"];
    NSMutableAttributedString * levelUpStr2 = [[NSMutableAttributedString alloc] initWithString:levelUp];
    NSMutableAttributedString * levelUpStr3 = [[NSMutableAttributedString alloc] initWithString:@"名"];
    [levelUpStr2 addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.87 green:0.27 blue:0.5 alpha:1] range:NSMakeRange(0,levelUpStr2.length)];
    [levelUpStr2 addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:NSMakeRange(0, levelUpStr2.length)];
    [levelUpStr1 appendAttributedString:levelUpStr2];
    [levelUpStr1 appendAttributedString:levelUpStr3];
    _levelUpLabel.font = [UIFont systemFontOfSize:13];
    _levelUpLabel.alpha = 0.87;
    _levelUpLabel.attributedText = levelUpStr1;
    [self addSubview:_levelUpLabel];
    
    _levelRankLabel = [[UILabel alloc]initWithFrame:CGRectMake(x / 2 - 65, y / 2 + 15, 180, 25)];
    NSMutableAttributedString * levelRankStr1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"位于%@榜第",rankUpCity]];
    NSMutableAttributedString * levelRankStr2 = [[NSMutableAttributedString alloc] initWithString:rankUp];
    NSMutableAttributedString * levelRankStr3 = [[NSMutableAttributedString alloc] initWithString:@"名"];
    [levelRankStr2 addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.87 green:0.27 blue:0.5 alpha:1] range:NSMakeRange(0,levelRankStr2.length)];
    [levelRankStr2 addAttribute: NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:NSMakeRange(0, levelRankStr2.length)];
    [levelRankStr1 appendAttributedString:levelRankStr2];
    [levelRankStr1 appendAttributedString:levelRankStr3];
    _levelRankLabel.font = [UIFont systemFontOfSize:13];
    _levelRankLabel.alpha = 0.87;
    _levelRankLabel.attributedText = levelRankStr1;
    [self addSubview:_levelRankLabel];
    
    _okBtn = [[UIButton alloc]initWithFrame:CGRectMake(x / 2 - 55, y / 2 + 70, 100, 45)];
    [_okBtn setTitle:@"好的" forState:UIControlStateNormal];
    [_okBtn setTitleColor:[UIColor colorWithRed:0.87 green:0.27 blue:0.5 alpha:1] forState:UIControlStateNormal];
    [_okBtn setBackgroundImage:[UIImage imageNamed:@"levelUP_btn"] forState:UIControlStateNormal];
    [_okBtn setBackgroundImage:[UIImage imageNamed:@"levelUP_btn_click"] forState:UIControlStateHighlighted];
    [_okBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_okBtn];
    
}


-(void)initView:(NSString *)level upDesc:(NSString *)upDesc{
    
    self.backgroundImage = [UIImage imageNamed:@"levelUP"];
    
    self.centerY = CGRectGetMidY(self.bounds) + 50;
    
    CGFloat x = self.bounds.size.width;
    CGFloat y = self.bounds.size.height;
    _levelLabel = [[UILabel alloc]initWithFrame:CGRectMake(x / 2 - 60, y / 3 - 10, 110, 25)];
    _levelLabel.text = [NSString stringWithFormat:@"Lv.%@",level];
    _levelLabel.textAlignment = NSTextAlignmentCenter;
    _levelLabel.textColor = [UIColor colorWithRed:1 green:0.95 blue:0.2 alpha:1];
    _levelLabel.font = [UIFont boldSystemFontOfSize:26];
    [self addSubview: _levelLabel];
    
    _levelUpLabel = [[UILabel alloc]initWithFrame:CGRectMake(x / 2 - 72, y / 2 - 5, 170, 60)];
    _levelUpLabel.font = [UIFont systemFontOfSize:13];
    _levelUpLabel.alpha = 0.87;
    _levelUpLabel.lineBreakMode = NSLineBreakByWordWrapping;
    _levelUpLabel.numberOfLines = 0;
    NSMutableAttributedString * levelUpStr1 = [[NSMutableAttributedString alloc] initWithString:upDesc];
    [levelUpStr1 addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,11)];
    [levelUpStr1 addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.87 green:0.27 blue:0.5 alpha:1] range:NSMakeRange(11,5)];
    
    _levelUpLabel.attributedText = levelUpStr1;
    [self addSubview:_levelUpLabel];
   
    
    _okBtn = [[UIButton alloc]initWithFrame:CGRectMake(x / 2 - 55, y / 2 + 70, 100, 45)];
    [_okBtn setTitle:@"好的" forState:UIControlStateNormal];
    [_okBtn setTitleColor:[UIColor colorWithRed:0.87 green:0.27 blue:0.5 alpha:1] forState:UIControlStateNormal];
    [_okBtn setBackgroundImage:[UIImage imageNamed:@"levelUP_btn"] forState:UIControlStateNormal];
    [_okBtn setBackgroundImage:[UIImage imageNamed:@"levelUP_btn_click"] forState:UIControlStateHighlighted];
    [_okBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_okBtn];
    
}


-(void)show{
    [self addToWindow];
    self.alpha = 0;
    [UIView animateWithDuration:2.0 animations:^{
        self.alpha = 1;
    }];
}
-(void)hide{
    [self removeFromWindow];
}

@end
